from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp

from base_page import BaseHandler
from data import Message

class PostMessageHandler(BaseHandler):
  # Set authenticating logic.
  def CanEdit(self, user, msg):
    if not user:
      return False
    if user.is_sys_admin or user.is_request_mod:
      return True
    if msg == None:
      # everyone can add a new message
      return True
    if msg.creator.key() == user.key():
      # One can always edit its own message
      return True
    else:
      return False

  def ProcessGet(self):
    self.Setup('post_message.html', required_registered=True)
    msg = self.GetEntityByKeyName('mid')

    # Check priviledges
    if self.CanEdit(self.user, msg):
      self.template_values['can_edit'] = '1'
    else:
      # require at least editing priviledge to view this page
      self.RedirectLogin()

    if msg != None:  # edit an existing message
      self.template_values['msg'] = msg
      self.template_values['post_action'] = 'Action.edit'
    else:  # add a new message to a request
      req = self.GetEntityByKeyName('qid')
      if req == None:  # error
        self.RedirectAndRaise('/')
      self.template_values['req'] = req
      self.template_values['post_action'] = 'Action.add'
    self.Render()

  def ProcessPost(self):
    self.Setup('')  # need no template
    if self.post_action == 'Action.add':
      self.HandleAdd()
    elif self.post_action == 'Action.edit':
      self.HandleEdit()
    elif self.post_action == 'Action.delete':
      self.HandleDelete()
    elif self.post_action == 'Action.approve':
      self.HandleApprove()
    else:  # default handler
      self.JsonErrorReturn('Unknown action.')

  def HandleAdd(self):
    if not self.CanEdit(self.user, None):
      # TODO: more friendly alert
      self.RedirectLogin()

    qid = self.request.get('qid')
    msg = Message()
    msg.creator = self.user
    msg.key_str = qid
    msg.message = self.request.get('message')
    msg.put()
    # done
    self.RedirectAndRaise('/viewrequest?qid=%s' % qid)

  def HandleEdit(self):
    msg = self.GetEntityByKeyName('mid')
    if msg == None:
      # TODO: more friendly alert
      self.RedirectAndRaise('/')

    if not self.CanEdit(self.user, msg):
      # TODO: more friendly alert
      self.RedirectLogin()

    msg.message = self.request.get('message')
    msg.put()
    self.RedirectAndRaise('viewrequest?qid=%s' % msg.key_str)

  def HandleDelete(self):
    msg = self.GetEntityByKeyName('mid')
    if msg == None:
      # TODO: more friendly alert
      self.RedirectAndRaise('/')

    if not self.CanEdit(self.user, msg):
      # TODO: more friendly alert
      self.RedirectLogin()

    #TODO: remove this parameter from the page
    redirect_url = self.request.get('redirect_url')
    msg.delete()
    self.RedirectAndRaise(redirect_url)

